/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.module.log.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.framework.common.utils.Func;
import com.easy.module.log.entity.SysOperLog;
import com.easy.module.log.mapper.SysOperLogMapper;
import com.easy.module.log.query.SysOperLogQuery;
import com.easy.module.log.service.ISysOperLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 操作日志 Service业务
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
    /**
     * 获取查询对象
     *
     * @param query
     * @return
     */
    @Override
    public LambdaQueryWrapper<SysOperLog> getWrapper(SysOperLogQuery query) {
        LambdaQueryWrapper<SysOperLog> wrapper = Wrappers.<SysOperLog>lambdaQuery();
        if (Func.isNotEmpty(query)) {
            wrapper.eq(Func.isNotEmpty(query.getId()),SysOperLog::getId, query.getId());
            wrapper.like(Func.isNotEmpty(query.getModule()),SysOperLog::getModule, query.getModule());
            wrapper.eq(Func.isNotEmpty(query.getType()),SysOperLog::getType, query.getType());
            wrapper.like(Func.isNotEmpty(query.getDescription()),SysOperLog::getDescription, query.getDescription());
            wrapper.eq(Func.isNotEmpty(query.getMethod()),SysOperLog::getMethod, query.getMethod());
            wrapper.eq(Func.isNotEmpty(query.getDevice()),SysOperLog::getDevice, query.getDevice());
            wrapper.eq(Func.isNotEmpty(query.getOs()),SysOperLog::getOs, query.getOs());
            wrapper.eq(Func.isNotEmpty(query.getStatus()),SysOperLog::getStatus, query.getStatus());
            wrapper.eq(Func.isNotEmpty(query.getUsername()),SysOperLog::getUsername, query.getUsername());
            if (Func.isNotEmpty(query.getParams())) {
                String beginTime = MapUtil.getStr(query.getParams(), "beginTime");
                String endTime = MapUtil.getStr(query.getParams(), "endTime");
                if (Func.isNotEmpty(beginTime)) {
                    wrapper.ge(SysOperLog::getOperAt, beginTime);
                }
                if (Func.isNotEmpty(endTime)) {
                    wrapper.le(SysOperLog::getOperAt, endTime);
                }
            }
        }
        wrapper.orderByDesc(SysOperLog::getId);
        return wrapper;
    }

    /**
     * 查询操作日志记录列表
     *
     * @param query 操作日志记录
     * @return 操作日志记录
     */
    @Override
    public List<SysOperLog> queryList(SysOperLogQuery query) {
        return list(getWrapper(query));
    }

    /**
     * 新增操作日志记录
     *
     * @param dto 操作日志记录
     * @return 结果
     */
    @Async
    @Override
    public CompletableFuture<Boolean> addAsync(SysOperLog dto) {
        dto.setOperAt(new Date());
        return CompletableFuture.completedFuture(save(dto));
    }

    /**
     * 清空日志
     */
    @Override
    public void clean() {
        baseMapper.clean();
    }

}
